package m.jclecai.com.dialog;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import java.io.BufferedReader;
import java.io.InputStreamReader;

import butterknife.BindView;
import butterknife.OnClick;
import m.jclecai.com.R;
import m.jclecai.com.adapter.SelectListAdapter;
import m.jclecai.com.listener.Callback;
import m.jclecai.com.utils.http.FastOk;

/**
 * Created by ZD on 2017/5/24.
 */

public class SelectlistDialog extends BaseDialog {

    @BindView(R.id.lv_left)
    ListView lvLeft;
    @BindView(R.id.lv_right)
    ListView lvRight;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    private Callback callback;
    JSONArray left, right;
    String leftSelectName = "", rightSelectName = "";

    public enum TYPE {
        CITY, BANK
    }

    TYPE type = TYPE.BANK;
    Context mContext;

    public SelectlistDialog(Context context, boolean fromButton, TYPE type
            , String leftSelectName, String rightSelectName) {
        super(context, fromButton);
        this.type = type;
        this.mContext = context;
        this.rightSelectName = rightSelectName;

        this.leftSelectName = leftSelectName;
        initViews();
    }

    public void setTitle(String title) {
        tvTitle.setText(title);
    }

    public void setListener(Callback callback) {
        this.callback = callback;
    }

    void initViews() {
        setContentView(R.layout.dialog_select_list);
        setCancelable(true);
        setCanceledOnTouchOutside(false);
        left = new JSONArray();
        right = new JSONArray();
        String name = this.type.toString().toLowerCase();
        String fromAssets = getFromAssets(name + ".txt");
        JSONObject jsonObject = FastOk.get(fromAssets, JSONObject.class);
        JSONArray province = jsonObject.getJSONArray("province");
        final JSONArray city = jsonObject.getJSONArray("city");
        JSONArray bank = jsonObject.getJSONArray("bank");
        if (type.equals(TYPE.BANK)) {
            left.addAll(bank);
            LinearLayout.LayoutParams l = (LinearLayout.LayoutParams) lvLeft.getLayoutParams();
            l.weight = 1;
            LinearLayout.LayoutParams r = (LinearLayout.LayoutParams) lvRight.getLayoutParams();
            r.weight = 0;
        } else {
            left.addAll(province);
            right.addAll(city.getJSONObject(0).getJSONArray("item"));
            LinearLayout.LayoutParams l = (LinearLayout.LayoutParams) lvLeft.getLayoutParams();
            l.weight = 2;
            LinearLayout.LayoutParams r = (LinearLayout.LayoutParams) lvRight.getLayoutParams();
            r.weight = 3;
        }
        final SelectListAdapter rAdapter = new SelectListAdapter(getContext(), right, TYPE.CITY, rightSelectName);
        final SelectListAdapter lAdapter = new SelectListAdapter(getContext(), left, TYPE.BANK, leftSelectName);
        lvLeft.setAdapter(lAdapter);
        lvLeft.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                leftSelectName =  (left.getString(position));
                if (type.equals(TYPE.BANK)) {
                    dismiss();
                    if (callback != null)
                        callback.callback(leftSelectName);
                } else {
                    right.clear();
                    right.addAll(city.getJSONObject(position).getJSONArray("item"));
                    rAdapter.notifyDataSetChanged();
                }
                lAdapter.setIndex(leftSelectName);
            }
        });

        lvRight.setAdapter(rAdapter);
        lvRight.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                rightSelectName = (right.getString(position));
                leftSelectName = TextUtils.isEmpty(leftSelectName) ?  (left.getString(0)) :  (leftSelectName);
                if (callback != null)
                    callback.callback(leftSelectName + "_" + rightSelectName);
                dismiss();
                rAdapter.setIndex(rightSelectName);
            }
        });
    }

    public String getFromAssets(String fileName) {
        try {
            InputStreamReader inputReader = new InputStreamReader(mContext.getAssets().open(fileName));
            BufferedReader bufReader = new BufferedReader(inputReader);
            String line = "";
            String Result = "";
            while ((line = bufReader.readLine()) != null)
                Result += line;
            return Result;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }

    @OnClick(R.id.tv_back)
    void tvBack() {
        dismiss();
    }
}
